unit GroupRightsForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UDF, uADGUIxIntf, uADGUIxFormsWait,
  uADStanIntf, uADStanOption, uADStanError, uADPhysIntf, uADStanDef,
  uADStanPool, uADStanAsync, uADPhysManager, uADStanParam, uADDatSManager,
  uADDAptIntf, uADDAptManager, Vcl.StdCtrls, Vcl.Grids, Vcl.DBGrids, Data.DB,
  uADCompDataSet, uADCompClient, uADCompGUIx, uADPhysMySQL, uADPhysIB,
  Vcl.Menus;

type
  TfmGroupRights = class(TForm)
    ADPhysIBDriverLink1: TADPhysIBDriverLink;
    ADPhysMySQLDriverLink1: TADPhysMySQLDriverLink;
    ADGUIxWaitCursor1: TADGUIxWaitCursor;
    ADConnection1: TADConnection;
    dsGroups: TDataSource;
    adtGroups: TADTable;
    dsGroupRights: TDataSource;
    adtGroupRights: TADTable;
    DBGrid1: TDBGrid;
    Label1: TLabel;
    Label2: TLabel;
    DBGrid2: TDBGrid;
    btnInitRights: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnInitRightsClick(Sender: TObject);
    procedure ProcessMenu(Menu: TMenuItem);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
    CurrentGroupID: integer;
  public
    { Public declarations }
  end;

var
  fmGroupRights: TfmGroupRights;

implementation

uses
  MainForm;

{$R *.dfm}

procedure TfmGroupRights.btnInitRightsClick(Sender: TObject);
begin
  CurrentGroupID := adtGroups.FieldByName('ID').AsInteger;
  ProcessMenu(fmMain.MainMenu1.Items);
end;

procedure TfmGroupRights.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := True;
  FreeAndNil(fmGroupRights);
end;

procedure TfmGroupRights.FormCreate(Sender: TObject);
var
  ADPhysDriverLink: TADPhysDriverLink;
  DriverID: string;
begin
  DriverID := DBDriverID;
  if DriverID = 'Firebird' then
    ADPhysDriverLink := ADPhysIBDriverLink1;
  if DriverID = 'MySQL' then
    ADPhysDriverLink := ADPhysMySQLDriverLink1;
  SetDBInfo(ADConnection1, ADPhysDriverLink);
  adtGroups.Active := True;
  adtGroupRights.Active := True;
end;

procedure TfmGroupRights.ProcessMenu(Menu: TMenuItem);
var
  i: integer;
begin
  with adtGroupRights do
  begin
    Insert;
    for i := 0 to Menu.Count - 1 do
    begin
      FieldByName('GROUPID').AsInteger := CurrentGroupID;
      FieldByName('ITEMNAME').AsString := Menu.Items[i].Name;
      FieldByName('ITEMVALUE').AsBoolean := False;
      if Menu.Count > 0 then
        ProcessMenu(Menu[i]);
    end;
    Post;
  end;
end;

end.
